const express = require("express")
const router = express.Router()
const time = require("../../utils/time")
const db = require("../../sqlServer/dbConnect")

// 菜单权限-- 查询
router.post("/", (req, res) => {
    const { values,pageConfig,doName } = req.body;
    const {page,pageSize} = pageConfig
    let sql = values === null ? "select * from menu limit ?,?;select * from menu" : "select * from menu where value=? or level=? limit ?,?;select * from menu where value=? or level=? "
    const data = values === null ? [(Number(page)-1)*Number(pageSize),Number(pageSize)] : [values.name,values.level,(Number(page)-1)*Number(pageSize),Number(pageSize),values.name,values.level]
    db.sqlQuery(sql, data, (err, result) => {
        if (err) {
            console.log(err)
            res.send({
                msg: "查询失败",
                status: 203
            })
        } else {
            res.send({
                msg: "查询成功",
                status: 200,
                data: result[0],
                total:result[1].length
            })
        }
    })
    db.sqlQuery("insert into log(doName,doInfo,doTime) values(?,?,?)",[doName,"菜单查询",time.getData()])
})

router.post("/all", (req, res) => {
    let sql =  "select * from menu"
    db.sqlQuery(sql, (err, result) => {
        if (err) {
            console.log(err)
            res.send({
                msg: "查询失败",
                status: 203
            })
        } else {
            res.send({
                msg: "查询成功",
                status: 200,
                data: result
            })
        }
    })
})

// 菜单-- 添加
router.post("/add", (req, res) => {
    const { value, code, parentId, level, path,doName } = req.body
    let sql = "insert into menu(value,code,parentId,level,path) values(?,?,?,?,?)"
    const data = [value, code, parentId, level, path]
    db.sqlQuery("select * from menu where code=?", [code], (err, result) => {
        if (result.length !== 0) {
            res.send({
                status: 203,
                msg: "菜单标识重复,添加失败"
            })
            return
        } else {
            db.sqlQuery(sql, data, (err) => {
                if (err) {
                    console.log(err)
                    res.send({
                        msg: "添加失败",
                        status: 203
                    })
                } else {
                    res.send({
                        msg: "添加成功",
                        status: 200
                    })
                }
            })
        }
    })
    db.sqlQuery("insert into log(doName,doInfo,doTime) values(?,?,?)",[doName,"菜单添加",time.getData()])
})

// 菜单-- 编辑
router.post("/edit", (req, res) => {
    const { value, code, parentId, level, path, Id ,doName} = req.body
    let sql = "update menu set value=?,code=?,parentId=?,level=?,path=? where Id=?"
    const data = [value, code, parentId, level, path, Id]
    db.sqlQuery("select * from menu where code=?", [code], (err, result) => {
        if (result.length === 2) {
            res.send({
                status: 203,
                msg: "菜单标识重复,添加失败"
            })
            return
        } else {
            db.sqlQuery(sql, data, (err) => {
                if (err) {
                    console.log(err)
                    res.send({
                        msg: "编辑失败",
                        status: 203
                    })
                } else {
                    res.send({
                        msg: "编辑成功",
                        status: 200
                    })
                }
            })
        }
    })
    db.sqlQuery("insert into log(doName,doInfo,doTime) values(?,?,?)",[doName,"菜单编辑",time.getData()])
})

// 菜单-- 删除
router.post("/delete", (req, res) => {
    const { Id, role,doName } = req.body
    let sql = "delete from menu where Id=?"
    const data = [Id]
    db.sqlQuery(sql, data, (err) => {
        if (err) {
            console.log(err)
            res.send({
                msg: "删除失败",
                status: 203
            })
        } else {
            res.send({
                msg: "删除成功",
                status: 200
            })
            // 同时删除该角色拥有该权限的Id
            db.sqlQuery("select power from role where name=?", [role], (err, result) => {
                if (err) { console.log(err) }
                else {
                    const s = result[0].power.split(",")
                    s.map((item, index) => {
                        if (Number(item) === Id) {
                            s.splice(index, 1)
                        }
                    })
                    db.sqlQuery("update role set power=? where name=?", [s.join(","), role], (err) => {
                        if (err) { console.log(err) }
                    })
                }
            })
        }
    })
    db.sqlQuery("insert into log(doName,doInfo,doTime) values(?,?,?)",[doName,"菜单删除",time.getData()])
})

module.exports = router